#!/bin/bash
cd `dirname $0`
DEPLOY_DIR=`pwd`

JAVA_NAME="ratel-web.jar --spring.profiles.active=product"
JAVA_PATH="$DEPLOY_DIR"/

JAVA_STRAP=" $JAVA_PATH$JAVA_NAME "
JAVA_PROTOCOL=" -Ddubbo.protocol.host=127.0.0.1 "
JAVA_OPTS=" -Djava.awt.headless=true -Djava.net.preferIPv4Stack=true "

SERVER_NAME="$JAVA_NAME"
LOGS_DIR=$DEPLOY_DIR/logs
if [ ! -d $LOGS_DIR ]; then
    mkdir $LOGS_DIR
fi
STDOUT_FILE=$LOGS_DIR/stdout.log


case "$1" in

	start)

	PIDS=`ps -ef|grep java|grep "$JAVA_STRAP"|grep -v grep|awk '{print $2}'`
	if [ -n "$PIDS" ]; then
	    echo "ERROR: The $JAVA_STRAP already started!"
	    echo "PID: $PIDS"
	    exit 1
	fi

	JAVA_DEBUG_OPTS=""
	if [ "$2" = "debug" ]; then
	    JAVA_DEBUG_OPTS=" -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n "
	fi
	JAVA_JMX_OPTS=""
	if [ "$2" = "jmx" ]; then
	    JAVA_JMX_OPTS=" -Dcom.sun.management.jmxremote.port=1099 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false "
	fi
	JAVA_MEM_OPTS=""
	BITS=`java -version 2>&1 | grep -i 64-bit`
	if [ -n "$BITS" ]; then
     	JAVA_MEM_OPTS="-server -XX:+UseG1GC -Xms2g -Xmx2g -Xss512K -XX:MetaspaceSize=256m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=logs/ -XX:+PrintGCDateStamps -verbose:gc -XX:+PrintGCDetails -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=10 -Xloggc:logs/gc-%t.log"
	else
	    JAVA_MEM_OPTS=" -server -Xms1g -Xmx1g -XX:PermSize=128m -XX:SurvivorRatio=2 -XX:+UseParallelGC "
	fi
	echo -e "Starting the $SERVER_NAME ...\c"
	if [ "$2" = "log" ] || [ "$2" = "debug" ]; then
	    nohup java -jar $JAVA_OPTS $JAVA_MEM_OPTS $JAVA_DEBUG_OPTS $JAVA_JMX_OPTS $JAVA_STRAP $JAVA_PROTOCOL > $STDOUT_FILE 2>&1 &
        else
	    nohup java -jar $JAVA_OPTS $JAVA_MEM_OPTS $JAVA_DEBUG_OPTS $JAVA_JMX_OPTS $JAVA_STRAP $JAVA_PROTOCOL > /dev/null 2>&1 &
	fi
	echo -e "Starting the $JAVA_OPTS $JAVA_MEM_OPTS $JAVA_DEBUG_OPTS $JAVA_JMX_OPTS $JAVA_STRAP $JAVA_PROTOCOL ...\c"
	COUNT=0
	while [ $COUNT -lt 1 ]; do    
	    echo -e ".\c"
	    sleep 1 
	    COUNT=`ps -ef|grep java|grep "$JAVA_STRAP"|grep -v grep| awk '{print $2}' | wc -l`
	    if [ $COUNT -gt 0 ]; then
	        break
	    fi
	done

	echo "OK!"
	PIDS=`ps -ef|grep java|grep "$JAVA_STRAP"|grep -v grep| awk '{print $2}'`
	echo "PID: $PIDS"
	##echo "STDOUT: $STDOUT_FILE"
        ;;

    stop)

	PIDS=`ps -ef|grep java|grep "$JAVA_STRAP"|grep -v grep| awk '{print $2}'`
        if [ -z "$PIDS" ]; then
	    echo "ERROR: The $JAVA_STRAP does not started!"
	    exit 1
	fi


	echo -e "Stopping the $SERVER_NAME ...\c"
	for PID in $PIDS ; do
	    kill $PID > /dev/null 2>&1
	done

	COUNT=0
	while [ $COUNT -lt 1 ]; do    
	    echo -e ".\c"
	    sleep 1
	    COUNT=1
	    for PID in $PIDS ; do
	        PID_EXIST=`ps -f -p $PID | grep java`
	        if [ -n "$PID_EXIST" ]; then
	            COUNT=0
	            break
	        fi
	    done
	done

	echo "OK!"
	echo "PID: $PIDS"
        ;;

    restart)

        $0 stop
        sleep 2
        $0 start
        echo "=== restart $SERVICE_NAME"
        ;;

    *)

        echo $"Usage: $0 {start|stop|restart}"
	exit 2
        ;;

esac
exit 0


